package com.dimaidt.girl.controller;

import com.dimaidt.girl.domain.Girl;
import com.dimaidt.girl.domain.Result;
import com.dimaidt.girl.repository.GirlRepository;
import com.dimaidt.girl.service.GirlService;
import com.dimaidt.girl.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author : Alex Hu
 * date : 2020/2/26 上午07:08
 * description :
 */
@RestController
public class GirlController {
    private final static Logger logger = LoggerFactory.getLogger(GirlController.class);
    @Autowired
    GirlRepository girlRepository;

    /**
     * 版本1：直接在Controller写逻辑，不推荐
     * 获得girl列表
     *
     * @return
     */
    @GetMapping("/girls")
    public List<Girl> listGirls() {
        return girlRepository.findAll();
    }

    @Autowired
    GirlService girlService;

    /**
     * 版本2：将Controller与Service分开
     * Service写法有两种：
     * a.Service中直接实现业务逻辑，@Autowired注入实现类
     * b.Service中定义接口，ServiceImpl实现具体业务逻辑，@Autowired注入接口类
     * 两种写法对Controller无影响，即@Autowired注入的类名不变，要不Service实现类要不Service接口类,与ServiceImpl无关
     * 获得girl列表
     *
     * @return
     */
    @GetMapping("/girls_v2")
    public List<Girl> listGirls2() {
        return girlService.getList();
    }

    @GetMapping("/girls/{age}")
    public List<Girl> listGirlByAge(@PathVariable("age") Integer age) {
        return girlService.findByAge(age);
    }

    //    @PostMapping("/girls")
    //    public Girl addGirl(
    //            @RequestParam("cupSize") String cupSize,
    //            @RequestParam("age") Integer age) {
    //
    //        Girl girl = new Girl();
    //        girl.setAge(age);
    //        girl.setCupSize(cupSize);
    //
    //        return girlRepository.save(girl);
    //    }

    /**
     * 重构上面的方法，参数为对象，但是前端调用居然可以不变！！
     *
     * @param girl
     * @return
     */
    @PostMapping("/girls")
    public Result addGirl(@Valid Girl girl, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(-1,
                    bindingResult.getFieldError().getDefaultMessage()
            );
        } else {
            return ResultUtil.success(girlRepository.save(girl));
        }
    }

    /**
     * 按id删除girl
     *
     * @param id
     */
    @DeleteMapping("/girl/{id}")
    public void deleteGirl(@PathVariable("id") Integer id) {

        girlRepository.deleteById(id);
    }

    /**
     * 按id更新girl信息
     *
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PutMapping("/girls/{id}")
    public Girl updateGirl(
            @PathVariable("id") Integer id,
            @RequestParam("cupSize") String cupSize,
            @RequestParam("age") Integer age) {

        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        girl.setCupSize(cupSize);

        return girlRepository.save(girl);
    }

    /**
     * 按id查询girl
     *
     * @param id
     * @return
     */
    @GetMapping("/girl/{id}")
    public Girl getGirl(@PathVariable("id") Integer id) {

        return girlRepository.findById(id).orElse(null);
    }

    @GetMapping("/girlage/{id}")
    public void getAgeMsg(@PathVariable("id") Integer id) throws Exception {
        girlService.getAgeMsg(id);
    }

}
